package com.openscorer;

import java.util.LinkedList;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

public class InterfaceBuilder extends ScorableVisitor {
	InterfaceBuilder(Activity context, Scorable root, ScoreDisplay display) {
		activity_ = context;
		stack_ = new LinkedList<ViewGroup>();
		display_ = display;
		visit(root);
	}
	void Add(View view) {
		if (stack_.isEmpty()) {
			root_ = view;
		} else {
			stack_.getLast().addView(view);
		}
	}
	View JustName(Scorable s) {
		TextView tv = new TextView(activity_);
		tv.setText(s.getName());
		return tv;
	}
	void PopStack() {
		stack_.removeLast();
	}
	
	public void endRepeated(Repeated r) {
		PopStack();
	}
	public void endStructure(Structure s) {
		PopStack();
	}
	public void visitEnumated(Enumerated e) {
		Add(JustName(e));
	}
	public void visitInt(Int i) {
		IntSetter i_view = new IntSetter(activity_, i, display_);
		Add(i_view.View());
	}
	public void visitRepeated(Repeated r) {
		TableLayout ll = new TableLayout(activity_);
		ll.setOrientation(TableLayout.VERTICAL);
		ll.addView(JustName(r));
		Add(ll);
		stack_.add(ll);
	}
	public void visitStructure(Structure s) {
		TableLayout ll = new TableLayout(activity_);
		ll.setOrientation(TableLayout.VERTICAL);
		ll.addView(JustName(s));
		Add(ll);
		stack_.add(ll);
	}
	View root() {
		if (stack_.isEmpty()) {
			return root_;
		} else {
			return stack_.getFirst();
		}
	}
	Activity activity_;
	View root_;
	LinkedList<ViewGroup> stack_;
	ScoreDisplay display_;
}
